package com.lcq.floatview;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class DragView extends ImageView {
    private float downX, downY;
    private int width, height;
    private final int screenWidth, screenHeight;
    private int l, t, r, b;
    boolean isDoLayout = false;
    private boolean isDrag = false;

    private OnClickCallback onClickCallback;

    public interface OnClickCallback {
        void onClick(View v);
    }

    public DragView(Context context, AttributeSet attrs) {
        super(context, attrs);
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        screenHeight = context.getResources().getDisplayMetrics().heightPixels - getStatusBarHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    public int getStatusBarHeight() {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isDoLayout) {
            doLayout(l, t, r, b);
        }
    }

    public void doLayout(int l, int t, int r, int b) {
        isDoLayout = true;
        this.l = l;
        this.t = t;
        this.r = r;
        this.b = b;
        this.layout(l, t, r, b);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (this.isEnabled()) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    isDrag = false;
                    downX = event.getX();
                    downY = event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    final float xDistance = event.getX() - downX;
                    final float yDistance = event.getY() - downY;
                    int l, r, t, b;
                    if (Math.abs(xDistance) > 10 || Math.abs(yDistance) > 10) {
                        isDrag = true;
                        l = (int) (getLeft() + xDistance);
                        r = l + width;
                        t = (int) (getTop() + yDistance);
                        b = t + height;
                        if (l < 0) {
                            l = 0;
                            r = l + width;
                        } else if (r > screenWidth) {
                            r = screenWidth;
                            l = r - width;
                        }
                        if (t < 0) {
                            t = 0;
                            b = t + height;
                        } else if (b > screenHeight) {
                            b = screenHeight;
                            t = b - height;
                        }

                        doLayout(l, t, r, b);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (getLeft() + getWidth() / 2 <= screenWidth / 2) {
                        doLayout(0, getTop(), getWidth(), getBottom());
                    } else {
                        doLayout(screenWidth - getWidth(), getTop(), screenWidth, getBottom());
                    }
                    setPressed(false);
                    if (!isDrag) {
                        if (onClickCallback != null) {
                            onClickCallback.onClick(this);
                        }
                    }
                case MotionEvent.ACTION_CANCEL:
                    setPressed(false);
                    break;
            }
            return true;
        }
        return false;
    }

    public OnClickCallback getOnClickCallback() {
        return onClickCallback;
    }

    public void setOnClickCallback(OnClickCallback onClickCallback) {
        this.onClickCallback = onClickCallback;
    }
}
